import math
import time
import execjs
from LoginDemo import phone,pwd
import requests

#     ----- 36氪金平台 -----  RSA 非对称性加密   通过公钥进行模拟加密
#           1、一般公钥的存在地方，网页源代码  -- js   -- 后端返回
#           2、该平台使用 时间戳  RSA 加密

url = "https://gateway.36kr.com/api/mus/login/byMobilePassword"

headers = {
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Origin": "https://www.36kr.com",
    "Referer": "https://www.36kr.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183",
}

def encroySing(value):
    jsFile = open('36ks-rsa加密.js', 'r', encoding='utf-8').read()
    return execjs.compile(jsFile).call('encrypt',value)

data = {
    "krtoken":"",
    "partner_id":"web",
    "timestamp":str(math.ceil(time.time() * 1000)),
    "param":{
        "countryCode":"86",
        "mobileNo":encroySing(phone),
        "password":encroySing(pwd)
            }
}
response = requests.post(url, headers=headers, json=data)
print(response.content.decode())